package com.airticketbooking.service.serviceImpl;

import com.airticketbooking.common.utils.BeanUtils;
import com.airticketbooking.po.Flight;
import com.airticketbooking.po.FlightSchedule;
import com.airticketbooking.query.FlightSchedulePageQuery;
import com.airticketbooking.vo.FlightScheduleVO;
import com.airticketbooking.mapper.FlightMapper;
import com.airticketbooking.mapper.ScheduleMapper;
import com.airticketbooking.service.ScheduleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import javassist.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
public class ShceduleServiceImpl implements ScheduleService {


	private final ScheduleMapper scheduleMapper;

	private final FlightMapper flightMapper;


	@Override
	public Page<FlightScheduleVO> list(FlightSchedulePageQuery flightSchedulePageQuery) {

		PageHelper.startPage(flightSchedulePageQuery.getPageNo(), flightSchedulePageQuery.getPageSize());


		Page<FlightScheduleVO> shcedulelist = scheduleMapper.list(flightSchedulePageQuery);

		return shcedulelist;

	}

	@Override
	public FlightScheduleVO getShceduleVOById(Integer shceduleId) throws NotFoundException {
		// 检查输入参数是否为 null
		if (shceduleId == null) {
			throw new IllegalArgumentException("调度ID不能为空");
		}

		// 从数据库获取 FlightShcedule 对象
		FlightSchedule shcedule = scheduleMapper.getById(shceduleId);

		// 检查是否成功获取调度信息
		if (shcedule == null) {
			throw new NotFoundException("调度信息未找到，ID: " + shceduleId);
		}

		// 获取与调度相关的 Flight 对象
		Flight flight = flightMapper.getFlightById(shcedule.getFlightId());

		// 检查是否成功获取航班信息
		if (flight == null) {
			throw new NotFoundException("航班信息未找到，航班ID: " + shcedule.getFlightId());
		}

		// 创建 FlightShceduleVO 对象并复制属性
		FlightScheduleVO flightScheduleVO = new FlightScheduleVO();
		BeanUtils.copyProperties(shcedule, flightScheduleVO);
		BeanUtils.copyProperties(flight, flightScheduleVO);

		return flightScheduleVO;
	}

	@Override
	public Page<FlightScheduleVO> getAll() {

		PageHelper.startPage(1, 10000);

		Page<FlightScheduleVO> shcedulelist = scheduleMapper.listAll();

		return shcedulelist;
	}

	@Override
	@Transactional
	public void modifyStatus(Integer id, Integer status) throws NotFoundException {

		if (id == null || status == null) {
			throw new IllegalArgumentException("参数不能为空");
		}

		FlightSchedule shcedule = scheduleMapper.getById(id);

		if (shcedule == null) {
			throw new NotFoundException("调度信息未找到，ID: " + id);
		}

		if (status != 1 && status != 2 && status != 3 && status != 4) {
			throw new IllegalArgumentException("状态值不正确");
		}


		scheduleMapper.modifyStatus(id, status);
	}


}
